Skip to content

feat(config): add reserved_context_size setting for configurable compaction trigger#639

Merged
stdrc merged 4 commits into
MoonshotAI:mainfrom
zerone0x:rc/auto-compact-threshold
Jan 21, 2026
Merged

feat(config): add reserved_context_size setting for configurable compaction trigger#639
stdrc merged 4 commits into
MoonshotAI:mainfrom
zerone0x:rc/auto-compact-threshold

Conversation

@zerone0x

Copy link
Copy Markdown
Contributor

Related Issue

Resolve #351

Description

This PR adds a configurable auto_compact_threshold setting that allows users to adjust when auto-compaction triggers.

Changes:

  • Added auto_compact_threshold field to LoopControl config with default value 0.8 (80%)
  • Valid range is 0.1-1.0 (10%-100%)
  • Updated KimiSoul to use percentage-based threshold instead of the previous fixed reserved_tokens approach
  • Added tests for the new configuration option including boundary validation

Usage:

Users can configure the threshold in their config.toml:

[loop_control]
auto_compact_threshold = 0.7  # trigger compaction at 70% context usage

This addresses the user request to be able to adjust the auto-compact trigger point based on their task requirements.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked the related issue, if any.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have run make gen-changelog to update the changelog.
  • I have run make gen-docs to update the user documentation.

🤖 Generated with Claude Code

…paction trigger

Allow users to configure the context usage threshold (0.1-1.0) that triggers
auto-compaction. Default is 0.8 (80%). This replaces the previous fixed
reserved_tokens approach with a percentage-based threshold.

Fixes MoonshotAI#351

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings January 18, 2026 18:08

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a configurable auto_compact_threshold setting to allow users to control when automatic context compaction triggers based on percentage of context usage, replacing the previous fixed token-based approach.

Changes:

  • Added auto_compact_threshold configuration field (default: 0.8, range: 0.1-1.0) to LoopControl
  • Replaced fixed RESERVED_TOKENS constant with percentage-based threshold in compaction logic
  • Added validation tests for the new configuration parameter

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/kimi_cli/config.py Adds auto_compact_threshold field to LoopControl with validation constraints
src/kimi_cli/soul/kimisoul.py Removes RESERVED_TOKENS constant and updates compaction logic to use percentage threshold
tests/test_config.py Adds tests for default value and boundary validation of auto_compact_threshold

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/kimi_cli/soul/kimisoul.py Outdated
):
logger.info("Context too long, compacting...")
threshold = self._loop_control.auto_compact_threshold
if self._context_usage >= threshold:

Copilot AI Jan 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code references self._context_usage but this property is not defined in the visible diff or the provided context. Verify that this property exists and correctly calculates the context usage percentage relative to self._runtime.llm.max_context_size.

Copilot uses AI. Check for mistakes.
@stdrc

stdrc commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

What about reserved_context_size? IMO what we really care about in compaction is not the percentage, it's the remained token count.

@zerone0x

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! You're right - the reserved token count is more important than the percentage, since the LLM needs a consistent amount of space for generation regardless of the total context size.

I can update the PR to use reserved_context_size instead:

class LoopControl(BaseModel):
    # ...
    reserved_context_size: int = Field(default=50_000, ge=1000)
    """Reserved token count that triggers auto-compaction. Default is 50000."""

And the compaction trigger would be:

if self._context.token_count + self._loop_control.reserved_context_size >= self._runtime.llm.max_context_size:

This keeps the original logic but makes the reserved token count configurable. Should I push this change?

@stdrc

stdrc commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

Should I push this change?

Yeah sure. That would be sweet. Thank you!

…eshold

Per maintainer feedback, switch from percentage-based threshold to
token-based reserved_context_size for auto-compaction. This better
reflects what matters: ensuring enough tokens remain for LLM response
generation regardless of total context size.

Changes:
- Rename auto_compact_threshold to reserved_context_size (default: 50000)
- Restore original token-based compaction logic
- Update tests accordingly

Co-Authored-By: Claude <noreply@anthropic.com>
@zerone0x

Copy link
Copy Markdown
Contributor Author

@stdrc Done! Pushed commit 8d0bd0b with the changes:

  • Renamed auto_compact_threshold to reserved_context_size (default: 50000 tokens)
  • Restored the original token-based compaction logic: context_tokens + reserved_context_size >= max_context_size
  • Updated tests accordingly

The config usage is now:

[loop_control]
reserved_context_size = 50000  # can adjust based on needs

stdrc added 2 commits January 21, 2026 13:05
…hold

Signed-off-by: Richard Chien <stdrc@outlook.com>
Signed-off-by: Richard Chien <stdrc@outlook.com>
@xxchan xxchan changed the title feat(config): add auto_compact_threshold setting for configurable compaction trigger feat(config): add reserved_context_size setting for configurable compaction trigger Jan 21, 2026
@stdrc
stdrc merged commit 3108621 into MoonshotAI:main Jan 21, 2026
11 checks passed
@zerone0x
zerone0x deleted the rc/auto-compact-threshold branch January 21, 2026 05:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide a configuration setting to adjust threshold for auto-compact trigger.

3 participants